/**
 * @author Zou Yu
 * create at: 2022/3/21 16:58
 */

public class Temp_1 {

    /**
     * mid = left + (right - left) / 2
     * 初始条件：left = 0, right = length-1 <br>
     * 终止条件：left > right <br>
     * 向左查找：right = mid-1 <br>
     * 向右查找：left = mid+1 <br>
     */

    int binarySearch(int[] nums, int target){
        if(nums == null || nums.length == 0) {
            return -1;
        }

        int left = 0;
        int right = nums.length - 1;

        while(left <= right) {
            // Prevent (left + right) overflow
            int mid = left + (right - left) / 2;  // or int mid = left ((right - left) >> 1)
            if(nums[mid] == target) { return mid; }
            else if(nums[mid] < target) { left = mid + 1; }
            else { right = mid - 1; }
        }

        // End Condition: left > right
        return -1;
    }

    public static void main(String[] args) {

    }
}